<?php
echo "name change start<br/>";
include_once('config.inc.php');

$link=mysql_connect($hostname,$username,$password);

$status=mysql_select_db("vtigercrm520");

$query="SELECT contactid,lastname FROM vtiger_contactdetails";

$mixnamesquery=mysql_query($query, $link);

if(!$mixnamesquery) {
		print("Query Error: ".mysql_error());
	}

   while($contactdata=mysql_fetch_assoc($mixnamesquery)) {
     print("fetching mixed name <br>");
     $mixedname=$contactdata['lastname'];
     echo "mixed name is: $mixedname <br/>";
     $namearr=explode(" ",$mixedname);
     if ($namearr[1]) {
    	  $name=array_shift($namearr); 
   	  $surname=implode(" ",$namearr);
   	  echo "name: $name surname: $surname<br>";
   	  $updatenamequery="UPDATE vtiger_contactdetails 
   	            SET firstname = '$name', lastname = '$surname' 
   	            WHERE contactid = $contactdata[contactid]";
   	  echo "Executing update<br/>";
   	  $updatequery=mysql_query($updatenamequery,$link);
   	  if(!$updatequery) {
			print("Query Error: ".mysql_error());
			}          
     } else {echo "No first name here<br/>";}
   }
   
echo "All done.";

?>